/*
 *  This file is part of Fax .NET.
 *
 *  Fax .NET is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation.
 * 
 *  Fax .NET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Fax .NET.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;

namespace Tourreau.Gilles.FaxDotNet
{
    /// <summary>
    /// Represent a read-only collection of fax jobs.
    /// </summary>
    public class FaxJobCollection : ReadOnlyCollection<FaxJob>
    {
        internal FaxJobCollection()
            : base(new List<FaxJob>())
        {
            
        }

        /// <summary>
        /// Add a <see cref="FaxJob"/> object in the collection.
        /// </summary>
        /// <param name="faxJob"></param>
        internal void Add(FaxJob faxJob)
        {
            int index;

            for (index = 0; index < this.Count && this[index].ID < faxJob.ID; index++) ;

            this.Items.Insert(index, faxJob);
        }

        /// <summary>
        /// Find a fax job by ID and return it.
        /// </summary>
        /// <param name="id">ID of fax job to be find.</param>
        /// <returns>The fax job found or null in otherwise.</returns>
        internal FaxJob GetJob(uint id)
        {
            foreach (FaxJob job in this)
            {
                if (job.ID == id)
                    return job;
            }

            return null;
        }

        /// <summary>
        /// Remove a fax job.
        /// </summary>
        /// <param name="id">ID of fax job to be delete.</param>
        internal void Remove(uint id)
        {
            this.Items.Remove(this.GetJob(id));
        }

        /// <summary>
        /// Get all fax jobs for the current user.
        /// </summary>
        /// <returns>All fax jobs of the current user.</returns>
        /// <remarks>The current user is obtained by <see cref="Environment.UserDomainName"/> and <see cref="Environment.UserName"/></remarks>
        public FaxJob[] GetJobsUser()
        {
            return this.GetJobsUser(string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName));
        }

        /// <summary>
        /// Get all fax jobs for an user.
        /// </summary>
        /// <param name="userName">Windows user name to be found (Domain\\UserName)</param>
        /// <returns>All fax jobs of the <paramref name="userName"/></returns>
        public FaxJob[] GetJobsUser(string userName)
        {
            List<FaxJob> jobs;

            jobs = new List<FaxJob>();

            foreach (FaxJob job in this)
            {
                if (string.Compare(job.UserName, userName, true, CultureInfo.CurrentCulture) == 0)
                    jobs.Add(job);
            }

            return jobs.ToArray();
        }
    }
}
